package com.yc.taixin.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yc.taixin.dao.YuyueDao;
import com.yc.taixin.mapper.YuyueMapper;
import com.yc.taixin.model.TSYuyue;
import com.yc.taixin.support.utils.NoImportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 逻辑层
 */
@Service
public class YuyueServiceImpl extends BaseService<TSYuyue, String> {

	@Autowired
	private YuyueDao yuyueDao;

	@Autowired
	private YuyueMapper yuyueMapper;

	//处理"list"
	private JSONArray getParamList(String restime) {
		List<String> dateList = new NoImportUtil().getWeekDay_yc(12, "MM月dd日 E");
		JSONArray array = new JSONArray();
		String[] str = null;
		for (String date : dateList) {
			JSONObject object = new JSONObject();
			str = date.split(" ");
			object.put("weekarray", str[1].replace("星期", "周"));
			object.put("date", str[0]);
			array.add(object);
		}
		return array;
	}

	//处理"time"列表
	private Object getParamTime(String restime) {
		List<String> m = yuyueDao.listYYtime();
		NoImportUtil util = new NoImportUtil();
		Map<Integer, String> iiMap = util.yuyueIiMap_yc();
		List<String> yymmdd = util.getWeekDay_yc(12, "yyyy-MM-dd");
		List<String> mmdd = util.getWeekDay_yc(12, "MM月dd日");
		JSONArray array = new JSONArray();
		for (int i = 0; i < yymmdd.size(); i++) {
			JSONArray arrayTwo = new JSONArray();
			for (Map.Entry<Integer, String> entry : iiMap.entrySet()) {
				JSONObject object = new JSONObject();
				object.put("ii", entry.getKey());
				object.put("date", mmdd.get(i));
				String yyiiParam = yymmdd.get(i) + " " + entry.getValue(); // "2020-06-11 14:30-15:30",
				object.put("rescount", yyiiParam);
				//判断是否已经预约
				String timeStr = this.timeStrHandle(entry.getKey(), yyiiParam,
						entry.getValue(), m.contains(yyiiParam), Objects.equals(yyiiParam, restime));
				object.put("time", timeStr);
				arrayTwo.add(object);
			}
			array.add(arrayTwo);
		}
		return array;
	}

	//处理"time"参数
	private String timeStrHandle(int ii, String yyiiParam, String iiParam, Boolean isYuyue, Boolean isSeleced) {
		String str = "";
		if (isYuyue) {
			str += "<li class='bg-gray' style='margin:2px 0;'><div class='weui-flex bg-gray'><div class='weui-flex-item'><svg class='icon f-gray' aria-hidden='true'><use xlink:href='#icon-clockfill'></use></svg>" +
					iiParam +
					"<span class='weui-badge' style='position: absolute;top:3px;right:2px;background-color:#999;'>满员</span></div><div class='weui-flex-item'><span class='dayu-badge bg-gray right'></span></div></div></li>";
		} else {
			if (isSeleced) {
				str += "<li class='ctime actvd' ";
			} else {
				str += "<li class='ctime' ";
			}
			str += "onclick='choosetime(this," + ii + ")' " + "data='" +
					yyiiParam + "' style='margin:2px 0;'>" + "<div class='weui-flex' ><div class=' weui-flex-item' >" +
					"<svg class=' icon f-green'  aria-hidden=' true' >" +
					"<use xlink:href=' #icon-clockfill' ></use></svg>" + iiParam +
					"<span class=' weui-badge'  style=' position: absolute;top:3px;right:2px;' >1</span></div><div class=' weui-flex-item' ><span class=' dayu-badge bg-green right' ></span></div></div></li>";
		}
		return str;
	}

	public JSONObject getYuyueConf(String restime) {
		JSONObject object = new JSONObject();
		object.put("list", this.getParamList(restime));
		object.put("title", "<span class='f-white'>选择时间</span>");
		object.put("time", this.getParamTime(restime));
		return object;
	}

	public List<String> listYYtime() {
		return yuyueDao.listYYtime();
	}

	//预约成功列表数据
	public List<TSYuyue> listSuccess(Map<String, Object> map) {
		return yuyueMapper.listSuccess(map);
	}

	//预约成功数量
	public Long countSuccess(Map<String, Object> map) {
		return yuyueMapper.countSuccess(map);
	}


}
